package com.mall.service.item;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.item.MallItemPriceService;
import com.mall.custom.dao.read.MallItemPriceReadMapper;
import com.mall.custom.dao.write.MallItemPriceWriteMapper;
import com.mall.pojo.MallItemPrice;
import com.mall.pojo.MallItemPriceExample;
import com.mall.pojo.MallItemPriceExample.Criteria;
@Service
public class MallItemPriceServiceImpl implements MallItemPriceService {
	@Autowired
	private MallItemPriceReadMapper mallItemPriceReadMapper;
	@Autowired
	private MallItemPriceWriteMapper mallItemPriceWriteMapper;

	@Override
	public MallItemPrice getMallItemPriceByMallItemPriceId(Long mallItemPriceId) {
		if(mallItemPriceId == null || mallItemPriceId <= 0){
			return null;
		}
		return this.mallItemPriceReadMapper.selectByPrimaryKey(mallItemPriceId);
	}

	@Override
	public int insertMallItemPrice(MallItemPrice mallItemPrice) {
		mallItemPrice.setGmtCreate(new Date());
		mallItemPrice.setGmtModify(new Date());
		return this.mallItemPriceWriteMapper.insert(mallItemPrice);
	}

	@Override
	public int modifyMallItemPrice(MallItemPrice mallItemPrice) {
		mallItemPrice.setGmtModify(new Date());
		return this.mallItemPriceWriteMapper.updateByPrimaryKey(mallItemPrice);
	}

	@Override
	public List<MallItemPrice> getMallItemPriceListByExample(MallItemPriceExample mallItemPriceExample) {
		return this.mallItemPriceReadMapper.selectByExample(mallItemPriceExample);
	}

	@Override
	public MallItemPrice getMallItemPriceByLevelAndKey(Long level, Integer onlyKey,Long mallItemId,Integer status) {
		MallItemPriceExample example = new MallItemPriceExample();
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		if(level != null && level > 0){
			criteria.andLevelEqualTo(level);
		}
		
		criteria.andMallItemIdEqualTo(mallItemId);
		
		if(status != null){
			criteria.andStatusEqualTo(status);
		}
		List<MallItemPrice> mallItemPriceList = this.mallItemPriceReadMapper.selectByExample(example);
		if(mallItemPriceList == null || mallItemPriceList.size() <= 0){
			return null;
		}
		return mallItemPriceList.get(0);
	}

	@Override
	public List<MallItemPrice> getMallItemPriceByItemIdAndKey(Integer onlyKey, Long mallItemId,Integer status) {
		MallItemPriceExample example = new MallItemPriceExample();
		example.setOrderByClause("level asc");
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		if(mallItemId != null && mallItemId >0){
			criteria.andMallItemIdEqualTo(mallItemId);
		}
		if(status != null ){
			criteria.andStatusEqualTo(status);
		}
		return this.mallItemPriceReadMapper.selectByExample(example);
	}

	@Override
	public Long insertMallItemPriceList(List<MallItemPrice> mallItemPriceList) {
		return this.mallItemPriceWriteMapper.betchInsertMallItemPrice(mallItemPriceList);
	}

	@Override
	public Integer replaceMallItemPriceList(List<MallItemPrice> mallItemPriceList) {
		// TODO Auto-generated method stub
		return this.mallItemPriceWriteMapper.replaceMallItemPrice(mallItemPriceList);
	}
	
}
